<template>
  <el-container>
    <el-header style="height: 105px;">
      <el-row :gutter="24" style="padding-top: 10px">
        <el-col :span="6">
          <el-input v-model="infoQuery.employeeName" placeholder="请输入员工名称"/>
        </el-col>
        <el-col :span="6">
          <el-select v-model="infoQuery.departmentId" placeholder="部门">
            <el-option
              v-for="item in depData"
              :key="item.departmentId"
              :label="item.departmentName"
              :value="item.departmentId"
            />
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-select v-model="infoQuery.positionId" placeholder="职务">
            <el-option
              v-for="item in posData"
              :key="item.positionId"
              :label="item.positionName"
              :value="item.positionId"
            />
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button type="success" round @click="$router.push('/info/add')">添加一个数据</el-button>
        </el-col>
      </el-row>
      <el-row :gutter="24" style="padding-top: 10px">
        <el-col :span="6">
          <el-select v-model="infoQuery.employeeEducation" placeholder="学历">
            <el-option label="大专及以下" value="1"/>
            <el-option label="本科" value="2"/>
            <el-option label="硕士" value="3"/>
            <el-option label="博士" value="4"/>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-select v-model="infoQuery.employeeStatus" placeholder="状态">
            <el-option label="在职" value="0"/>
            <el-option label="离职" value="1"/>
            <el-option label="请假" value="2"/>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-select v-model="infoQuery.employeeSex" placeholder="性别">
            <el-option label="男" value="1"/>
            <el-option label="女" value="2"/>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" round @click="search">查询</el-button>
          <el-button round @click="resetInfoQuery">重置</el-button>
        </el-col>
      </el-row>
    </el-header>
    <el-main>
      <div>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column
            prop="employeeId"
            label="id"
            width="65"
          />
          <el-table-column
            prop="employeeName"
            label="员工姓名"
          />
          <el-table-column
            prop="employeeEducation"
            label="员工学历"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.employeeEducation === 1">大专及以下</span>
              <span v-else-if="scope.row.employeeEducation === 2">本科</span>
              <span v-else-if="scope.row.employeeEducation === 3">硕士</span>
              <span v-else-if="scope.row.employeeEducation === 4">博士</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="positionName"
            label="员工职位"
            width="160"
          />
          <el-table-column
            prop="departmentName"
            label="员工部门"
            width="100"
          />
          <el-table-column
            prop="inductionTime"
            label="入职时间"
            width="200"
            :formatter="dateFormatter"
          />
          <el-table-column
            prop="employeeSex"
            label="员工性别"
            width="80"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.employeeSex === '1'">男</span>
              <span v-else-if="scope.row.employeeSex === '2'">女</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="employeeStatus"
            label="员工状态"
            width="100"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.employeeStatus === 0">在职</span>
              <span v-else-if="scope.row.employeeStatus === 1">离职</span>
              <span v-else-if="scope.row.employeeStatus === 2">请假</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="200"
          >
            <template slot-scope="scope">
              <el-button type="primary" @click.native="upd(scope.row.employeeId)">修改</el-button>
              <el-button type="danger" @click.native="del(scope.row.employeeId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block" style="margin-top: 20px;">
        <el-pagination
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15]"
          :page-size="infoQuery.pageUtil.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="infoQuery.pageUtil.count"
        />
      </div>
    </el-main>
  </el-container>
</template>

<script>

import moment from 'moment'
import { getPage, del } from '@/api/info'
import { getDep } from '@/api/dep'
import { getPos } from '@/api/pos'

export default {
  name: 'Info',
  data() {
    return {
      // 查询条件
      infoQuery: {
        employeeName: '',
        departmentId: '',
        positionId: '',
        employeeEducation: '',
        employeeStatus: '',
        employeeSex: '',
        pageUtil: {
          pageIndex: 1,
          count: 0,
          pageSize: 10
        }
      },
      // 表格数据
      tableData: [],
      // 部门数据
      depData: [],
      // 职位数据
      posData: []
    }
  },
  watch: {
    $route: {}
  },
  created() {
  },
  mounted() {
    this.init()
  },
  destroyed() {
  },
  methods: {
    init() {
      this.getPage()
      this.getPos()
      this.getDep()
    },
    // 分页查询
    getPage() {
      getPage(this.infoQuery).then(res => {
        this.tableData = res.data.pageList
        this.infoQuery.pageUtil.count = res.data.count
      })
    },
    // 获取全部pos
    getPos() {
      getPos().then(res => {
        this.posData = res.data
      })
    },
    // 获取全部dep
    getDep() {
      getDep().then(res => {
        this.depData = res.data
      })
    },
    // 时间格式处理
    dateFormatter(row) {
      if (!row.inductionTime) {
        return ''
      }
      return moment(row.inductionTime).format('YYYY-MM-DD / HH:ss:mm')
    },
    // 重置查询数据
    resetInfoQuery() {
      this.infoQuery = {
        employeeName: '',
        departmentId: '',
        positionId: '',
        employeeEducation: '',
        employeeStatus: '',
        employeeSex: '',
        pageUtil: {
          pageIndex: 1,
          pageSize: 10
        }
      }
      this.getPage()
    },
    // 修改信息
    upd(row) {
      this.$router.push({
        path: '/info/upd',
        query: {
          employeeId: row
        }
      })
    },
    // 删除
    del(row) {
      del({ id: row }).then(res => {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.getPage()
          this.$message({
            type: 'success',
            message: res.data
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      })
    },
    // 查询点击事件
    search() {
      this.infoQuery.pageUtil.pageIndex = 1
      this.getPage()
    },
    // 改变页大小
    handleCurrentChange(val) {
      this.infoQuery.pageUtil.pageIndex = val
      this.getPage()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  //text-align: center;
  //line-height: 60px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  //text-align: center;
  //line-height: 160px;
}
</style>
